package com.wangzhixuan.controller;

import javax.validation.Valid;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wangzhixuan.commons.result.PageInfo;
import com.wangzhixuan.commons.utils.FileUtil;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.model.Role;
import com.wangzhixuan.model.TFile;
import com.wangzhixuan.service.ITFileService;
import com.wangzhixuan.commons.base.BaseController;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-06-11
 */
@Controller
@RequestMapping("/tFile")
public class TFileController extends BaseController {

    @Autowired private ITFileService tFileService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/tFile/tFileList";
    }
    
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TFile tFile, Integer page, Integer rows, String sort,String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Page<TFile> pages = getPage(pageInfo);
        pages = tFileService.selectPageFile(pages, tFile);
        pageInfo.setRows(pages.getRecords());
        pageInfo.setTotal(pages.getTotal());
        return pageInfo;
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/tFile/tFileAdd";
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid TFile tFile) {
        List<File> list=FileUtil.getFileList(tFile.getPath());
        if(list==null || list.size()==0){
        	return renderError("该路径下没有获取到文件信息，请检查路径是否正确！");
        }
        for(File file:list){
        	TFile temp=new TFile();
        	temp.setName(file.getName());
        	temp.setPath(file.getAbsolutePath());
        	temp.setSize(file.getFreeSpace());
        	temp.setCreateTime(new Date());
        	logger.info("Path:"+file.getPath());
        	logger.info("AbsolutePath:"+file.getAbsolutePath());
        	tFileService.insert(temp);
        }
        return renderSuccess("添加成功！");
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TFile tFile = new TFile();
        tFile.setId(id);
        boolean b = tFileService.updateById(tFile);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TFile tFile = tFileService.selectById(id);
        model.addAttribute("tFile", tFile);
        //查询标签列表
        List<Role> list=new ArrayList<Role>();
        Role r1=new Role();
        r1.setName("娱乐项目");
        list.add(r1);
        Role r2=new Role();
        r2.setName("mihui项目");
        list.add(r2);
        Role r3=new Role();
        r3.setName("applo项目");
        r3.setId(2L);
        list.add(r3);
        model.addAttribute("tags", list);
        return "admin/tFile/tFileEdit";
    }
    
    /**
     * 编辑
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid TFile tFile) {
        boolean b = tFileService.updateById(tFile);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
